﻿//
//  UfoSprite.m
//  StarBugs
//
//  Created by 진권 정 on 2/2/12.
//  Copyright 2012 antisword@playorca.com. All rights reserved.
//

#include "Sprite/UfoSprite.h"
#include "Util/ImageManager.h"
#include "Util/ModuleController.h"

using namespace cocos2d;

UfoSprite::~UfoSprite()
{
    
}

UfoSprite::UfoSprite(cocos2d::CCPoint pt)
{
    winSize = CCSize(320, 480);
    mainSprite = new CCSprite();
    mainSprite->init();
    mainSprite->autorelease();
    mainSprite->setPosition(pt);
    addChild(mainSprite);
    
    CCSprite *sprite = ImageManager::getSpriteWithXml2(kImagesheet_GameSource, "ufo");
    mainSprite->addChild(sprite);
    
    for(int i = 0; i < 3; i++)
    {
        ufoinSprite[i] = ImageManager::getSpriteWithXml2(kImagesheet_GameSource, "ufo_in");
        ufoinSprite[i]->setPosition(ccp(0, -10 - 5*i));
        ufoinSprite[i]->setOpacity(150);
        mainSprite->addChild(ufoinSprite[i]);
        
        ufooutSprite[i] = ImageManager::getSpriteWithXml2(kImagesheet_GameSource, "ufo_out");
        ufooutSprite[i]->setPosition(ccp(0, -13 - 10*i));
        ufooutSprite[i]->setOpacity(150);
        ufooutSprite[i]->setVisible(false);
        mainSprite->addChild(ufooutSprite[i]);
    }
    setStateIn(true);
//    schedule(schedule_selector(UfoSprite::tick),0.2);
//    cnt = 0;
    
    setVisible(false);
    isEnable = false;
}

//当传入的参数是false时，游戏顶端的ufo实例将添加左右来回移动的动作，传入true时，将停止动作。
void UfoSprite::setStateIn(bool flg)
{
	
    if(isEnable == false || flg == stateIn) //原： if(isEnable == false)
        return;
    
    stateIn = flg;
    for(int i = 0; i < 3; i++)
    {
        ufoinSprite[i]->setVisible(flg);
        ufooutSprite[i]->setVisible(!flg);
    }
    
    if(flg)
    {
        mainSprite->stopAllActions();
    }
    else
    {
        mainSprite->setRotation(0);
        float time = (mainSprite->getPosition().x/ModuleController::getImageScale() - 15) * 3 / (float)(winSize.width - 30);
        CCMoveTo* move1 = CCMoveTo::create(time, ccp(15 * ModuleController::getImageScale(), mainSprite->getPosition().y));
        CCMoveTo* move2 = CCMoveTo::create(3, ccp((winSize.width - 15) * ModuleController::getImageScale(), mainSprite->getPosition().y));
        CCMoveTo* move3 = CCMoveTo::create(3, ccp(15 * ModuleController::getImageScale(), mainSprite->getPosition().y));
        CCAction *action1 = CCSequence::create(move2, move3, NULL);
        CCRepeat *repeat = CCRepeat::create((CCActionInterval *)action1, 1000);
        
        CCAction *action2 = CCSequence::create(move1, repeat, NULL);
        
        
//        CCRotateTo* rot1 = CCRotateTo::create(1, 85);
//        CCRotateTo* rot2 = CCRotateTo::create(1, -85);
//        CCAction* action = CCSequence::create(rot1, rot2, NULL);
//        CCRepeatForever *repeat = CCRepeatForever::create((CCActionInterval *)action);
        mainSprite->runAction(action2);
    }
}

void UfoSprite::tick(float dt)
{
    cnt = (cnt + 1)%4;
    int tmp;
    if(stateIn)
    {
        tmp = 3 - cnt;
        for(int i = 0; i < 3; i++)
        {
            if(tmp == i)
                ufoinSprite[i]->setOpacity(255);
            else
                ufoinSprite[i]->setOpacity(100);
                
        }
    }
    else
    {
        for(int i = 0; i < 3; i++)
        {
            if(cnt == i)
                ufooutSprite[i]->setOpacity(255);
            else
                ufooutSprite[i]->setOpacity(100);
            
        }

    }
}

void UfoSprite::setAngle(float angle)
{
    if(mainSprite)
        mainSprite->setRotation(angle);
}

void UfoSprite::pause()
{
    pauseSchedulerAndActions();
    if(mainSprite)
        mainSprite->pauseSchedulerAndActions();
}

void UfoSprite::resume()
{
    resumeSchedulerAndActions();
    if(mainSprite)
        mainSprite->resumeSchedulerAndActions();
}

void UfoSprite::setEnable()
{
    if(isEnable)
        return;
    setVisible(true);
    isEnable = true;
//    setStateIn(true);
    stateIn = true;
    schedule(schedule_selector(UfoSprite::tick),0.2);
    cnt = 0;
}